Skip to content

Method: param(String, byte)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package com.sun.codemodel;
19:
20: import java.lang.annotation.Annotation;
21: import java.util.LinkedHashMap;
22: import java.util.Map;
23:
24: /**
25: * Represents an annotation on a program element.
26: * <p>
27: * FIX to com.sun.codemodel.JAnnotationUse supporting JFieldRef as annotation values (constants)
28: */
29: public final class JAnnotationUse extends JAnnotationValue {
30:
31: /**
32: * The {@link Annotation} class
33: */
34: private final JClass clazz;
35:
36: /**
37: * Map of member values.
38: */
39: private Map<String, JAnnotationValue> memberValues;
40:
41: JAnnotationUse(JClass clazz) {
42: this.clazz = clazz;
43: }
44:
45: private JCodeModel owner() {
46: return clazz.owner();
47: }
48:
49: private void addValue(String name, JAnnotationValue annotationValue) {
50: // Use ordered map to keep the code generation the same on any JVM.
51: // Lazily created.
52: if (memberValues == null)
53: memberValues = new LinkedHashMap<>();
54: memberValues.put(name, annotationValue);
55: }
56:
57: /**
58: * Adds a member value pair to this annotation
59: *
60: * @param name The simple name for this annotation
61: * @param value The boolean value for this annotation
62: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
63: */
64: public JAnnotationUse param(String name, boolean value) {
65: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
66: return this;
67: }
68:
69: /**
70: * Adds a member value pair to this annotation
71: *
72: * @param name The simple name for this annotation
73: * @param value The byte member value for this annotation
74: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
75: */
76: public JAnnotationUse param(String name, byte value) {
77: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
78: return this;
79: }
80:
81: /**
82: * Adds a member value pair to this annotation
83: *
84: * @param name The simple name for this annotation
85: * @param value The char member value for this annotation
86: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
87: */
88: public JAnnotationUse param(String name, char value) {
89: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
90: return this;
91: }
92:
93: /**
94: * Adds a member value pair to this annotation
95: *
96: * @param name The simple name for this annotation
97: * @param value The double member value for this annotation
98: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
99: */
100: public JAnnotationUse param(String name, double value) {
101: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
102: return this;
103: }
104:
105: /**
106: * Adds a member value pair to this annotation
107: *
108: * @param name The simple name for this annotation
109: * @param value The float member value for this annotation
110: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
111: */
112: public JAnnotationUse param(String name, float value) {
113: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
114: return this;
115: }
116:
117: /**
118: * Adds a member value pair to this annotation
119: *
120: * @param name The simple name for this annotation
121: * @param value The long member value for this annotation
122: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
123: */
124: public JAnnotationUse param(String name, long value) {
125: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
126: return this;
127: }
128:
129: /**
130: * Adds a member value pair to this annotation
131: *
132: * @param name The simple name for this annotation
133: * @param value The short member value for this annotation
134: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
135: */
136: public JAnnotationUse param(String name, short value) {
137: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
138: return this;
139: }
140:
141: /**
142: * Adds a member value pair to this annotation
143: *
144: * @param name The simple name for this annotation
145: * @param value The int member value for this annotation
146: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
147: */
148: public JAnnotationUse param(String name, int value) {
149: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
150: return this;
151: }
152:
153: /**
154: * Adds a member value pair to this annotation
155: *
156: * @param name The simple name for this annotation
157: * @param value The String member value for this annotation
158: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
159: */
160: public JAnnotationUse param(String name, String value) {
161: //Escape string values with quotes so that they can
162: //be generated accordingly
163: addValue(name, new JAnnotationStringValue(JExpr.lit(value)));
164: return this;
165: }
166:
167: /**
168: * Adds a member value pair to this annotation
169: * For adding class values as param
170: *
171: * @param name The simple name for this annotation
172: * @param value The annotation class which is member value for this annotation
173: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
174: * @see #param(String, Class)
175: */
176: public JAnnotationUse annotationParam(String name, Class<? extends Annotation> value) {
177: JAnnotationUse annotationUse = new JAnnotationUse(owner().ref(value));
178: addValue(name, annotationUse);
179: return annotationUse;
180: }
181:
182: /**
183: * Adds a member value pair to this annotation
184: *
185: * @param name The simple name for this annotation
186: * @param value The enum class which is member value for this annotation
187: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
188: */
189: public JAnnotationUse param(String name, final Enum<?> value) {
190: addValue(name, new JAnnotationValue() {
191: public void generate(JFormatter f) {
192: f.t(owner().ref(value.getDeclaringClass())).p('.').p(value.name());
193: }
194: });
195: return this;
196: }
197:
198: /**
199: * Adds a member value pair to this annotation
200: *
201: * @param name The simple name for this annotation
202: * @param value The JEnumConstant which is member value for this annotation
203: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
204: */
205: public JAnnotationUse param(String name, JEnumConstant value) {
206: addValue(name, new JAnnotationStringValue(value));
207: return this;
208: }
209:
210: /**
211: * Adds a member value pair to this annotation
212: * <p>
213: * This can be used for e.g to specify
214: * <pre>
215: * @XmlCollectionItem(type=Integer.class);
216: * </pre>
217: * For adding a value of {@code Class<? extends Annotation>}
218: *
219: * @param name The simple name for this annotation param
220: * @param value The class type of the param
221: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
222: * @see #annotationParam(String, Class)
223: */
224: public JAnnotationUse param(String name, final Class<?> value) {
225: addValue(name, new JAnnotationStringValue(
226: new JExpressionImpl() {
227: @Override
228: public void generate(JFormatter f) {
229: f.p(value.getName().replace('$', '.'));
230: f.p(".class");
231: }
232: }));
233: return this;
234: }
235:
236: /**
237: * Adds a member value pair to this annotation based on the
238: * type represented by the given JType
239: *
240: * @param name The simple name for this annotation param
241: * @param type the JType representing the actual type
242: * @return The JAnnotationUse. More member value pairs can be added to it using the same or the overloaded methods.
243: */
244: public JAnnotationUse param(String name, JType type) {
245: JClass cls = type.boxify();
246: addValue(name, new JAnnotationStringValue(cls.dotclass()));
247: return this;
248: }
249:
250: /**
251: * Adds a member value pair which is of type array to this annotation
252: *
253: * @param name The simple name for this annotation
254: * @return The JAnnotationArrayMember. For adding array values
255: * @see JAnnotationArrayMember
256: */
257: public JAnnotationArrayMember paramArray(String name) {
258: JAnnotationArrayMember arrayMember = new JAnnotationArrayMember(owner());
259: addValue(name, arrayMember);
260: return arrayMember;
261: }
262:
263: /**
264: * This can be used to add annotations inside annotations
265: * for e.g @XmlCollection(values= @XmlCollectionItem(type=Foo.class))
266: *
267: * @param clazz The annotation class to be included
268: * @return The JAnnotationUse that can be used as a member within this JAnnotationUse
269: * @deprecated use {@link JAnnotationArrayMember#annotate}
270: */
271: public JAnnotationUse annotate(Class<? extends Annotation> clazz) {
272: return new JAnnotationUse(owner().ref(clazz));
273: }
274:
275: @Override
276: public void generate(JFormatter f) {
277: f.p('@').g(clazz);
278: if (memberValues != null) {
279: f.p('(');
280: boolean first = true;
281:
282: if (isOptimizable()) {
283: // short form
284: f.g(memberValues.get("value"));
285: } else {
286: for (Map.Entry<String, JAnnotationValue> mapEntry : memberValues.entrySet()) {
287: if (!first) {
288: f.p(',');
289: }
290: f.p(mapEntry.getKey()).p('=').g(mapEntry.getValue());
291: first = false;
292: }
293: }
294: f.p(')');
295: }
296: }
297:
298: private boolean isOptimizable() {
299: return memberValues.size() == 1 && memberValues.containsKey("value");
300: }
301:
302: public JAnnotationUse param(String name, JFieldRef value) {
303: addValue(name, new JAnnotationStringValue(value));
304: return this;
305: }
306: }